package repo

import (
	"hxy/repo/dag"
	mc "hxy/repo/microservice"
	"hxy/repo/model"
	. "hxy/repo/servise"
	"io"
	"net/http"

	"github.com/emicklei/go-restful/v3"
)

func CreateGitHandler() http.Handler {
	wsContainer := restful.NewContainer()
	wsContainer.EnableContentEncoding(true)
	ws := new(restful.WebService).
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON)
	ws.Path("/git")
	ws.Route(
		ws.GET("/hello").To(hello),
	)
	ws.Route(
		ws.POST("/repo/create").To(HandleCreateGitModel).
			Reads(model.GitModel{}).
			Writes(model.GitModel{}),
	)
	ws.Route(
		ws.GET("/repo/get").To(HandlerGetAllGitModel).
			Writes([]model.GitModel{}),
	)
	ws.Route(
		ws.GET("/repo/getbyid/{gitid}").To(HandlerGetGitModelByid).
			Writes(model.GitModel{}),
	)
	ws.Route(
		ws.DELETE("/repo/delete/{id}").To(HandlerDeleteGitModel),
	)
	/// Git Auth
	ws.Route(
		ws.POST("/auth").To(HandleCreateAuth).
			Reads(model.GitAuth{}).
			Writes(model.GitAuth{}),
	)
	ws.Route(
		ws.GET("/auth").To(HandlerGetAllGitAuth).Writes([]model.GitAuth{}),
	)
	ws.Route(
		ws.DELETE("/auth/delete/{id}").To(HandlerDeleteGitAuth),
	)
	ws.Route(
		ws.GET("/monitor").To(HandleMapManager),
	)
	ws.Route(
		ws.GET("/log/{logid}").To(HandlerLogWebsocket),
	)

	ws.Route(
		ws.GET("/logdocker").To(HandlerDockerLogsocket),
	)
	ws.Route(
		ws.GET("/citask/all").To(HandlerGetAllTask),
	)
	ws.Route(
		ws.GET("/citask/gitid/{gitid}").To(HandleGetTaksbyGitid),
	)
	ws.Route(
		ws.GET("/dockertask/{hash}").To(HanderDockerTaskbyHash),
	)
	ws.Route(
		ws.GET("/project/submodule/list/{gitid}").To(mc.HandlerSubModuleGet),
	)
	ws.Route(
		ws.POST("/project/submodule/dockerfile").
			To(mc.HandlerDockerfileDelivery).
			Reads(mc.DockerfileRequest{}).
			Writes(mc.DockerfielResponse{}),
	)
	ws.Route(
		ws.POST("/project/submodule/createdockerfile").
			To(mc.HandlerDockerfileCreate).
			Reads(mc.DockerfileCreate{}),
	)
	ws.Route(
		ws.POST("/project/submodule/gendocker").
			To(mc.HandlerAutoGenDockerfile).
			Reads(mc.DockerfileRequest{}).
			Writes(mc.DockerfielResponse{}),
	)
	ws.Route(
		ws.POST("/project/submodule/genyaml").
			To(mc.HandlerAutoGenK8sYaml).
			Reads(mc.GenCodeRequest{}).
			Writes(mc.GenCodeRequest{}),
	)

	ws.Route(
		ws.POST("/dagtask/create").
			To(dag.HandlerDagTaskCreate).
			Reads(dag.DagTask{}).
			Writes(dag.DagTask{}),
	)
	ws.Route(
		ws.GET("/dagtask/get/{gitid}").To(dag.HandlerDagTaskGet),
	)

	wsContainer.Add(ws)
	return wsContainer
}

func hello(req *restful.Request, resp *restful.Response) {
	io.WriteString(resp, "world")
}
